package com.spark;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/5/22.
 */
public class HDFSUtill {


    public static Configuration conf = new Configuration();


    public static String PATH = "hdfs://10.200.73.90:9000";


    public static void main(String[] args) throws IOException {


        String hdfs = PATH + "/readme.md";


    }

    /**
     * 创建目录
     *
     * @param dir
     * @throws IOException
     */
    public void createDir(String dir) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(PATH), conf);
        fs.mkdirs(new Path(dir));
    }

    /**
     * 删除目录或文件
     *
     * @param dir
     * @throws IOException
     */
    public static void deleteDir(String dir) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(PATH), conf);
        fs.delete(new Path(dir), true);
    }


    /**
     * 写文件
     *
     * @param hdfs
     * @param content
     * @throws IOException
     */
    public static void writeFile(String hdfs, String content) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
        hdfsOutStream.write(content.getBytes("UTF-8"));
        hdfsOutStream.close();
        fs.close();
    }

    public static void writeLine(String hdfs, List<String> content) throws IOException {
        StringBuffer buffer = new StringBuffer();
        for (String line : content) {
            buffer.append(line).append("\n");
        }

        writeFile(hdfs, buffer.toString());

    }


    /**
     * 读文件
     *
     * @param hdfs
     * @throws IOException
     */
    public String ReadFile(String hdfs) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        FSDataInputStream hdfsInStream = fs.open(new Path(hdfs));
        String res = IOUtils.toString(hdfsInStream);
        fs.close();
        return res;
    }

    /**
     * 遍历所有目录
     *
     * @param hdfs
     * @return
     * @throws IOException
     */
    public static List<String> getAllFile(String hdfs) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        FileStatus[] fileStatuses = fs.listStatus(new Path(hdfs));
        List<String> files = new ArrayList<String>();
        for (FileStatus fileStatus : fileStatuses) {
            files.add(fileStatus.getPath().getName());
        }
        return files;
    }


}
